package com.pic.mgt.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.action.UnitMonitorControlAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class UnitMonitorControlCommandPerformer extends
		AbstractCommandPerformer {
	
	private final String commandName = "mntctl";
	private final String commandDescritpion = "control unit(both business and message) monitor(start or stop).";
	
	private UnitMonitorControlAction unitMonitorControlAction;

	@Override
	protected void addOption(Options options) {
		options.addOption("str", false, "star unit (both business and message) monitor.");
		options.addOption("stp", false, "stop unit (both business and message) monitor.");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("str")) {
			boolean isStartUnitMonitorSuccessful =
				this.unitMonitorControlAction.startUnitMonitor();
			
			if (isStartUnitMonitorSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("stp")) {
			boolean isStopUnitMonitorSuccessful =
				this.unitMonitorControlAction.stopUnitMonitor();
			
			if (isStopUnitMonitorSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else {
			return
			super.getReplyMessageWithReplyCode(
					"did not specify control action (option: str or stp).",
					this.COMMAND_PERFORM_FAILED_CODE);
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}

	public void setUnitMonitorControlAction(
			UnitMonitorControlAction unitMonitorControlAction) {
		this.unitMonitorControlAction = unitMonitorControlAction;
	}

}
